package com.meevle.commands {
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.meevle.business.UserDelegate;
	import com.meevle.error.ErrorHandler;
	import com.meevle.events.RegistrationEvent;
	import com.meevle.model.ViewModelLocator;
	import com.meevle.vo.RegistrationVO;
	
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	
	public class RegistrationCommand implements ICommand, IResponder {
	
		private var registrationAttempt:RegistrationVO;
		
		public function RegistrationCommand() {
			
		}
		
		// ICommand interface method
		public function execute(event:CairngormEvent):void {
			var registrationEvent:RegistrationEvent = event as RegistrationEvent;
			registrationAttempt = registrationEvent.registrationAttempt;
			
			var delegate:UserDelegate = new UserDelegate(this);
			delegate.register( registrationAttempt );
		}
		
		//IResponder interface methods
		public function result(event:Object) : void {
			// REGISTRATION SUCCESS
			if(event.result == true) {
				ViewModelLocator.getInstance().registrationError = null;
				ViewModelLocator.getInstance().registrationStatus = true;
			} else {
				var error:Error = new Error("Sorry, the username you entered is not available");
				ViewModelLocator.getInstance().registrationError = error;
			}
			ViewModelLocator.getInstance().error = null;
		}
		
		public function fault(event:Object) : void {
			ErrorHandler.getInstance().handleError(event as FaultEvent);
		}	
	}
}